<template>
  <div>
    <el-dialog title="选择工序" :visible.sync="dialogTableVisible">
      <a-card :bordered="false">
        <div class="table-page-search-wrapper">
          <a-form layout="inline">
            <a-row :gutter="48">
              <a-col :md="12" :sm="24">
                <a-form-item label="工序名称">
                  <a-input v-model="queryParam.ProcessName" placeholder />
                </a-form-item>
              </a-col>
              <a-col :md="12" :sm="24">
                <a-button type="primary" @click="getDataList">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
                <a-button type="success" style="margin-left: 8px" @click="onConfirm">确定</a-button>
              </a-col>
            </a-row>
          </a-form>
        </div>

        <a-table
          ref="table"
          :columns="columns"
          :rowKey="row => row.Id"
          :dataSource="data"
          :pagination="pagination"
          :loading="loading"
          @change="handleTableChange"
          :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          :bordered="true"
          :scroll="{ y: height }"
          size="small"
        >
          <span slot="action" slot-scope="text, record">
            <template>
              <a @click="handleEdit(record.Id)">编辑</a>
              <a-divider type="vertical" />
              <a @click="handleDelete([record.Id])">删除</a>
            </template>
          </span>
        </a-table>
      </a-card>
    </el-dialog>
  </div>
</template>

<script>

const columns = [
  { title: '工序编号', dataIndex: 'ProcessCode', width: '100px' },
  { title: '工序名称', dataIndex: 'ProcessName', width: '200px' },
  { title: '备注', dataIndex: 'Remark1', width: '100px' }
]

export default {
  mounted () {
    this.getDataList()
  },
  data () {
    return {
      data: [],
      height: window.innerHeight - 310,
      pagination: {
        current: 1,
        pageSize: 20,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`
      },
      filters: {},
      sorter: { field: 'Id', order: 'asc' },
      loading: false,
      columns,
      queryParam: {},
      visible: false,
      selectedRowKeys: [],
      multipleSelection: [],
      dialogTableVisible: false
    }
  },
  methods: {
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post('/Aps/Aps_Process/GetDataList', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          const pagination = { ...this.pagination }
          pagination.total = resJson.Total
          this.pagination = pagination
        })
    },
    openForm () {
      this.multipleSelection = []
      this.dialogTableVisible = true
    },
    onConfirm () {
      this.multipleSelection = []
      if (this.selectedRowKeys.length > 0) {
        this.data.forEach(item => {
          this.selectedRowKeys.forEach(key => {
            if (item.Id === key) {
              this.multipleSelection.push(item)
            }
          })
        })
      }
      if (this.multipleSelection.length > 0) {
        this.dialogTableVisible = false
        this.changeChildFather(this.multipleSelection)
      } else {
        this.$message.error('请勾选要选择的物料!')
      }
    },
    changeChildFather (rows) {
      this.$emit('listenTochildEvent', rows)
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    hanldleAdd () {
      this.$refs.editForm.openForm()
    },
    handleEdit (id) {
      this.$refs.editForm.openForm(id)
    },
    handleDelete (ids) {
      var thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk () {
          return new Promise((resolve, reject) => {
            thisObj.submitDelete(ids, resolve, reject)
          }).catch(() => console.log('Oops errors!'))
        }
      })
    },
    submitDelete (ids, resolve, reject) {
      this.$http.post('/Aps/Aps_Process/DeleteData', ids).then(resJson => {
        resolve()

        if (resJson.Success) {
          this.$message.success('操作成功!')

          this.getDataList()
        } else {
          this.$message.error(resJson.Msg)
        }
      })
    }
  }
}
</script>
